package com.lx.controller;

import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;

import com.lx.biz.IUser;
import com.lx.biz.Indexbiz;
import com.lx.domain.AdminUser;
import com.lx.domain.SharingResource;
import com.lx.domain.Tree;
import com.lx.domain.Vote_list;

@Controller
public class IndexController {
	@Resource
	private IUser iUserimpl ;
	@Resource
	private Indexbiz iIndexbizimpl ;
	
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String index(Locale locale, Model model) {
		return "index" ;
	}
	
	@RequestMapping("login")
	public String login(Locale locale, Model model) {
		return "login" ;
	}
	
	@RequestMapping("logincheck")
	public String loginCheck(String username ,String password ,HttpServletRequest httpServletReques) {
		AdminUser user = iUserimpl.findUser(username, password);
		if (user==null) {
			httpServletReques.setAttribute("erro", "用户名或密码错误");
			return "login" ;
		}else {
			HttpSession session = httpServletReques.getSession(true) ;
			session.setAttribute("user", user);
			if (user.getRole()> 0) {
				return "redirect:/admin/main" ;
			}else {
				//跳入权限不足页面
				return "login" ; 
			}
			
		}
		
	}
	
	@RequestMapping("admin/tree")
	public @ResponseBody List<Tree> TreeList(String id ,HttpServletRequest httpServletRequest){
		AdminUser user = (AdminUser)httpServletRequest.getSession().getAttribute("user") ;
		return iIndexbizimpl.treelist(id,user.getRole()) ;
	}
	
	@RequestMapping("vote/votedatagrid")
	public @ResponseBody List<Vote_list> votelist(HttpServletRequest httpServletRequest){
		return iIndexbizimpl.votelist() ;
	}
	
	
	
}
